package com.lili.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.controller.request.XasAdListRequest;
import com.lili.controller.request.XasAdPositionListRequest;
import com.lili.controller.request.XasGoodsBrandRequest;
import com.lili.mapper.XasAdListMapper;
import com.lili.mapper.XasAdPositionListMapper;
import com.lili.po.XasAdList;
import com.lili.po.XasAdPositionList;
import com.lili.po.XasGoodsBrandList;
import com.lili.uitl.ShiroUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class XasAdService {
    @Autowired
    XasAdListMapper xasAdListMapper;
    @Autowired
    XasAdPositionListMapper xasAdPositionListMapper;
    @Autowired
    UserService userService;

    public IPage<XasAdList> selectXasAdList(XasAdListRequest xasAdListRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasAdListRequest, page);
//        LambdaQueryWrapper<XasAdList> lambdaQueryWrapper = Wrappers.lambdaQuery();
//        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasAdListRequest.getTitle()), XasAdList::getTitle, xasAdListRequest.getTitle());
        IPage iPage = xasAdListMapper.selectXasAdList(page, xasAdListRequest.getTitle(), xasAdListRequest.getCode());
        return iPage;
    }

    public List<XasAdList> selectAllXasAdList() {
        return xasAdListMapper.selectList(null);
    }

    @Transactional
    public void addXasAdList(XasAdList xasAdList) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasAdList.setCreateUser(userLoginName);
        xasAdList.setCreateTime(new Date());
        xasAdListMapper.insert(xasAdList);
    }

    @Transactional
    public void updateXasAdList(XasAdList xasAdList) {
        xasAdListMapper.updateById(xasAdList);
    }

    @Transactional
    public void delXasAdList(Integer id) {
        xasAdListMapper.deleteById(id);
    }


    public IPage<XasAdPositionList> selectXasAdPositionList(XasAdPositionListRequest xasAdPositionListRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasAdPositionListRequest, page);
        LambdaQueryWrapper<XasAdPositionList> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasAdPositionListRequest.getCode()), XasAdPositionList::getCode, xasAdPositionListRequest.getCode());
        IPage iPage = xasAdPositionListMapper.selectPage(page, lambdaQueryWrapper);
//        IPage<XasAdPositionList> iPage = xasAdPositionListMapper.selectXasAdPositionList(page, xasAdPositionListRequest.getTitle(), xasAdPositionListRequest.getCode());
        return iPage;
    }


    public List<XasAdPositionList> selectAllXasAdPositionList() {
        return xasAdPositionListMapper.selectList(null);
    }

    @Transactional
    public void addXasAdPositionList(XasAdPositionList xasAdPositionList) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasAdPositionList.setCreateUser(userLoginName);
        xasAdPositionList.setCreateTime(new Date());
        xasAdPositionListMapper.insert(xasAdPositionList);
    }

    @Transactional
    public void updateXasAdPositionList(XasAdPositionList xasAdPositionList) {
        xasAdPositionListMapper.updateById(xasAdPositionList);
    }

    @Transactional
    public void delXasAdPositionList(Integer id) {
        xasAdPositionListMapper.deleteById(id);
    }
}
